package ui.popup 
{
	import data.GCBuildingData;
	import flash.display.Shape;
	
	import events.GCEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import ui.button.GCButton;
	import ui.textfield.*;
	
	/**
	 * A popup that will allow a player to move idle workers from a city
	 * to their ship, or from the ship to the city.
	 * @author Roy
	 */
	public class GCWorkerTransferPopup extends Sprite 
	{
		
		private var _workersAtShip:int;
		private var _workersIdleAtCity:int;
		
		private var _description:GCTextField;
		private var _shipDisplay:GCStylizedText;
		private var _cityDisplay:GCStylizedText;
		
		private var _addToShipButton:GCButton;
		private var _addToCityButton:GCButton;
		private var _confirmButton:GCButton;
		
		/*
		 * Background 
		 */
		private var _backgroundImage:Sprite;
		
		public function GCWorkerTransferPopup() 
		{
			_workersAtShip = 0;
			_workersIdleAtCity = 0;
			
			_description = new GCTextField();
			_shipDisplay = new GCStylizedText();
			_cityDisplay = new GCStylizedText();
			
			_addToShipButton = new GCButton(onAddToShip);
			_addToCityButton = new GCButton(onAddToCity);
			
			_confirmButton = new GCButton(onConfirm);
			
			init();
		}
		
		public function openPopup(numAtShip:int, numIdleAtCity:int):void
		{
			this._workersAtShip = numAtShip;
			this._workersIdleAtCity = numIdleAtCity;
			refreshDisplay();
		}
		
		public function closePopup():void
		{
			
		}
		
		public function get workersIdleAtCity():int
		{
			return _workersIdleAtCity;
		}
		
		public function get workersAtShip():int
		{
			return _workersAtShip;
		}
		
		private function init():void
		{			
			/*var bg:Bitmap = GCResources.getResource("genericBackground");
			bg.width = 300;
			bg.height = 200;
			this.addChild(bg);*/
			
			_description.setDimensions(300, 50);
			_description.text = "Move workers to the ship to transport them to other cities, or move them off to put them to work in a city!";
			_description.y += 20;
			
			_shipDisplay.x = 0;
			_shipDisplay.y = _description.height + _description.y;
			_shipDisplay.setBgStyle(GCStylizedText.STYLE_ONE, 100, 30);
			
			_cityDisplay.x = 100;
			_cityDisplay.y = _description.height + _description.y;
			_cityDisplay.setBgStyle(GCStylizedText.STYLE_ONE, 100, 30);
			
			_addToShipButton.loadGraphic(GCResources.plate, 100, 30);
			_addToShipButton.loadText("Transfer to Ship");
			_addToShipButton.x = 0;
			_addToShipButton.y = _shipDisplay.y + _shipDisplay.height;
			
			_addToCityButton.loadGraphic(GCResources.plate, 100, 30);
			_addToCityButton.loadText("Transfer to City");
			_addToCityButton.x = _addToShipButton.x + _addToShipButton.width;
			_addToCityButton.y = _addToShipButton.y;
			
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			_confirmButton.x = _addToCityButton.x + _addToCityButton.width;
			_confirmButton.y = _addToShipButton.y;
			
			this.addChild(_description);
			this.addChild(_shipDisplay);
			this.addChild(_cityDisplay);
			this.addChild(_addToShipButton);
			this.addChild(_addToCityButton);
			this.addChild(_confirmButton);
			
			// Background
			_backgroundImage = GCResources.getMaskedCanvasBackground(300, 200);
			this.addChildAt(_backgroundImage, 0);
			
		}
		
		private function onConfirm(event:MouseEvent):void
		{
			trace("Close Worker transfer");
			dispatchEvent(new GCEvent(GCEvent.POPUP_CLOSED_EVENT));
		}
		
		private function onAddToShip(event:MouseEvent):void
		{
			if (_workersIdleAtCity > 0)
			{
				_workersIdleAtCity--;
				_workersAtShip++;
				refreshDisplay();
			}
		}
		
		private function onAddToCity(event:MouseEvent):void
		{
			if (_workersAtShip > 0)
			{
				_workersAtShip--;
				_workersIdleAtCity++;
				refreshDisplay();
			}
		}
		
		private function refreshDisplay():void
		{
			_shipDisplay.text = String(_workersAtShip);
			_cityDisplay.text = String(_workersIdleAtCity);
		}
	}

}